package com.example.testcomposeproject.pages.sideEffect.testDerivedStateOf

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.viewinterop.AndroidView
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.testcomposeproject.MyLogger
import com.example.testcomposeproject.PreViewData
import com.example.testcomposeproject.pages.copy.CopyViewModel
import com.example.testcomposeproject.ui.theme.AppTheme

/**
 * @author: hemin
 * @date: 2024/6/21
 * @Desc: 这个思路可以包装高德地图的MapView
 *
 */
@Preview(showBackground = true, device = PreViewData.PreviewPhoneSize)
@Composable
fun PreViewTestDerivedStateOfUI(modifier: Modifier = Modifier) {
    AppTheme {
        TestDerivedStateOfUI(modifier = modifier)

    }
}

/**
 * viewModel 用这个方法
 */
@Composable
fun TestDerivedStateOfScreen(
    modifier: Modifier = Modifier,
    viewModel: CopyViewModel = hiltViewModel()
) {
    TestDerivedStateOfUI(modifier)
}

/**
 * 真正的UI元素
 */
@Composable
fun TestDerivedStateOfUI(modifier: Modifier = Modifier) {
    var show by remember {
        mutableStateOf(false)
    }

    LaunchedEffect(show) {
        MyLogger.hLog().e("LaunchedEffect监听show的变化:$show")
    }
    Column(
        modifier = Modifier.fillMaxSize(),
        horizontalAlignment = androidx.compose.ui.Alignment.CenterHorizontally,
        verticalArrangement = androidx.compose.foundation.layout.Arrangement.Center
    ) {
        Button(onClick = { show = !show }) {
            Text(text = "点击")
        }

        Text(text = if (show) "显示" else "隐藏")
    }

}


